package com.mayuanfei.interceptor;

import com.mayuanfei.mapper.LogMapper;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO 类描述
 *
 * @author 老马
 * @date 2023-04-03 16:41
 */
public class MyInterceptor1 implements HandlerInterceptor {
    @Resource
    private LogMapper logMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor1的preHandle执行");
        // 返回true代表放行,可以继续到handler;否则无法执行我们的handler方法.
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor1的postHandle执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor1的afterCompletion执行");
        // 插入日志
        this.logMapper.insertLog();
    }
}